package com.task.submitter.controllers;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.task.submitter.TaskEnum;
import com.task.submitter.dto.UserDto;

@Controller
public class ShuffleController extends BaseController {

	@RequestMapping("/shuffle")
	public String shuffle(HttpServletRequest request, HttpServletResponse response) throws IOException{
		String user = getEmail(request, response);
		
		TaskOptions options = TaskOptions.Builder.withUrl("/shuffle/submit").param("name", user);
		QueueFactory.getQueue("tasks").add(options);
		
		return "redirect:/voltageflux";
	}
	
	@ResponseBody
	@RequestMapping("/shuffle/submit")
	public void shuffle(@ModelAttribute UserDto useDto) throws IOException{
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    Key userCtxKey = KeyFactory.createKey("UserContext", useDto.getName());
	    Query query = new Query("ImgKey", userCtxKey);
	    List<Entity> images = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
		for(int i = 0; i<100; i++){
			submitRandomTask(useDto.getName(), images);
		}
	}
	
	private void submitRandomTask(String user, List<Entity> images){
		TaskEnum task = TaskEnum.randomTask();
		QueueFactory.getQueue("tasks").add(getOptions(task, user, images));
	}
	
	private TaskOptions getOptions(TaskEnum task, String user, List<Entity> images){
		TaskOptions options = TaskOptions.Builder.withUrl(task.getValue());
		String dim  = String.valueOf(RandomUtils.nextInt(10, 20));
		String temp = String.valueOf(RandomUtils.nextInt(10, 500));
		String steps = String.valueOf(RandomUtils.nextInt(10, 100));
		Entity image = images.get(RandomUtils.nextInt(0, images.size()));
		String blobKey = String.valueOf(image.getProperty("key"));
		String nameId = String.valueOf(image.getProperty("nameId"));
		
		switch(task){
		
			case FLUX:		options.param("user", user).param("steps", steps).param("dim", dim).param("temperature", temp);
							break;
			case MEMBRANE:	options.param("user", user).param("steps", steps).param("dim", dim).param("temperature", temp);
							break;
			case ROTATE:	options.param("blobKey", blobKey).param("userName", user).param("nameId", nameId+"_AUTO").param("transform", "ROTATE");
							break;
			case FLIP:		options.param("blobKey", blobKey).param("userName", user).param("nameId", nameId+"_AUTO").param("transform", "FLIP");
							break;
			case LUCKY:		options.param("blobKey", blobKey).param("userName", user).param("nameId", nameId+"_AUTO").param("transform", "LUCKY");
							break;
		
		}
		
		return options;
	}
	
}
